<?php
namespace app\house\model;
use app\BaseModel;
use think\facade\Db;



class AdminHouse extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_house';
    protected $append = [

        'cate_text',
        'area_text',
        'img_text',

    ];
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 图片
     */
    public function getImgTextAttr($value,$data){
        $data = transImages($data,['imgs'],';');
        return $data['imgs'];
    }
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],';');
        return is_array($img[0])?$img[0]:[];
    }

    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getDesignImgAttr($value){

        $img = transImages([$value],['design_img'],',');

        return is_array($img[0])?$img[0]:[];
    }

    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getRealImgAttr($value){

        $img = transImages([$value],['real_img'],',');

        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgInfoAttr($value){
        $img = transImages([$value],['img_info'],';');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换视频
     */
    public function getVideoAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换视频风封面图
     */
    public function getVideoCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 10:18
     * @param $value
     * @return array|mixed
     * descption:转换vr封面图
     */
    public function getVrCoverAttr($value){
        $img = transImagesOne([$value],[$value]);
        return is_array($img)?$img[0]:$img;
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getAttrsAttr($value){
        $attr =  json_decode($value,true);
        if(isset($attr['物梯户比例业费'])){
            unset($attr['物梯户比例业费']);
        }
        return $attr;
    }

    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getBuildAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }

    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getNewHxAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getMonomerPathAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }
    /**
     * @param $value
     * @return mixed
     * 转换标签
     */
    public function getLabelsAttr($value){
        return  !empty($value)?explode(',',$value):[];
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getCommunityAttrsAttr($value){
        $attr =  json_decode($value,true);
        if(isset($attr['物梯户比例业费'])){
            unset($attr['物梯户比例业费']);
        }
        return $attr;
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 11:57
     * @param $value
     * @return mixed
     * descption:获取分类
     */
    public function getCateTextAttr($value,$data){
        $dis[] = ['id','=',$data['category']];
        $dis[] = ['status','>',-1];
        $cate = Db::name('longbing_card_house_category')->where($dis)->value('title');
        return $cate;
    }
    /**
     * User: chenniang
     * Date: 2019-09-16 14:04
     * @param $value
     * @param $data
     * @return void
     * descption:获取区域
     */
    public function getAreaTextAttr($value,$data){
        $dis[] = ['id','=',$data['area_id']];
        $dis[] = ['status','>',-1];
        $area  = Db::name('longbing_card_house_area')->where($dis)->value('title');
        return $area;
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房源列表
     */
    public function houseList($dis,$page = 10){
        $data = $this->where($dis)->status(-1)->order('top desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加房源
     */
    public function houseAdd($data){

        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;

        $house_staff = $data['house_staff'];
        unset($data['house_staff']);
        $res = $this->insert($data);

        $data['house_staff'] = $house_staff;
        //房源id
        $data['id'] = $this->getLastInsID();
        //编辑房源顾问
        $this->staffAdd($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑房源
     */
    public function houseUpdate($dis,$data){
        //更新时间
        $data['update_time'] = time();

        if(!empty($data['house_staff'])){
            $house_staff = $data['house_staff'];

        }
        if(isset($data['house_staff'])){
            unset($data['house_staff']);
        }
        //编辑房源
        $res = $this->where($dis)->update($data);
        if(!empty($house_staff)) {

            $data['house_staff'] = $house_staff;
        }
        //编辑房源顾问
        $this->staffAdd($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 13:53
     * @功能说明:添加房源推荐
     */
    public function staffAdd($data){

        $house_staff = new AdminHoseStaff();
        //删除所有
        $house_staff->staffDel(['house_id'=>$data['id']]);

//        dump($data);exit;
        if(!empty($data['house_staff'])){
            foreach ($data['house_staff'] as $v){
                //添加内容
                $insert = [
                    //uniacid
                    'uniacid' => $data['uniacid'],
                    //房源id
                    'house_id'=> $data['id'],
                    //员工id
                    'staff_id'=>$v
                ];
                $house_staff->staffAdd($insert);
            }
        }
        return true;

    }
    /**
     * @param $dis
     * 房源详情
     */
    public function houseInfo($dis){
        $data = $this->where($dis)->status(-1)->find();

        $house_staff = new AdminHoseStaff();

        if(!empty($data)){

            $data['house_staff'] = $house_staff->getStaff(['a.house_id'=>$data['id']]);
        }
        return !empty($data)?$data->toArray():$data;
    }



}